<template>
  <div>
    <el-form class="query-form" @submit.native.prevent :inline="true">
      <el-form-item>
        <select-address
          v-model="params.address_code"
          auto-top
          placeholder="所属区县"
          @change="getData()"
        ></select-address>
      </el-form-item>

      <el-form-item>
        <el-input
          v-model="params.customer_name"
          :clearable="true"
          placeholder="主体名称查询"
          @change="getData()"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-input
          v-model="params.lender_idcard"
          :clearable="true"
          placeholder="身份证号码"
          @change="getData()"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="getData()">查询</el-button>
      </el-form-item>

      <el-form-item class="float-right">
        <el-button
          type="primary"
          icon="el-icon-download"
          :loading="exportLoading"
          @click="handleDownload()"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>

    <c-table
      border
      class="table-sm"
      v-loading="loading"
      :stripe="false"
      :columns="tableColumns"
      :data="tableData"
    >
      <template slot="load_status" slot-scope="scope">
        <el-tag :type="scope.row.loadStyle">{{ scope.row.loadStatus }}</el-tag>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import SelectAddress from '@/components/select/SelectAddress'
import { enumRenewalStatus } from '@/credit/plugins/enum'

const SERVICE_NAME = 'credit'

export default {
  components: {
    SelectAddress,
  },
  props: {},
  data() {
    return {
      tableName: 'credit_visit_record_view',
      tableId: '',
      params: {
        index: 1,
        size: 15,
        customer_name: '',
        lender_idcard: '',
        address_code: '',
        // 'approval_register_status.eq': 1,
      },
      loading: false,

      tableData: [],
      tableCount: 0,
      tableColumns: [
        { attrs: { label: '所属地区', prop: 'address', minWidth: '160px' } },
        {
          attrs: { label: '主体名称', prop: 'customer_name', minWidth: '160px' },
          process: (v, row) => ({
            attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
          }),
        },
        { attrs: { label: '经营者姓名', prop: 'corp_name' } },
        { attrs: { label: '身份证号码', prop: 'lender_idcard' } },
        { attrs: { label: '记录', prop: 'visit_des' } },
        { attrs: { label: '提交人', prop: 'submit_person_name', align: 'center' } },
      ],
      enumRenewalStyle: {
        0: 'primary',
        1: 'success',
        2: 'info',
        3: 'warning',
        4: 'warning',
        5: 'danger',
        6: 'success',
      },
      submit_type: '',
      exportLoading: false,
    }
  },
  computed: {
    ...mapGetters(['org']),
    clientId() {
      return this.$store.getters.org.clientId
    },
  },
  watch: {},
  created() {
    let orgClass = this.org.orgClass
    let clientId = this.org.clientId
    this.submit_type =
      orgClass === 'org_gov_ny'
        ? '1'
        : orgClass === 'org_agr_leader'
        ? '3'
        : orgClass === 'org_supply_market'
        ? '4'
        : '2'
    if (
      orgClass === 'org_gov_ny' ||
      orgClass === 'org_agr_leader' ||
      orgClass === 'org_supply_market'
    ) {
      this.params.address_code = this.$utils.getAddressCode()
      this.params['submit_type.eq'] = this.submit_type
    } else if (orgClass === 'org_bank') {
      this.params['bank_id.eq'] = clientId
    } else {
      this.params['orWhere.service_id.eq'] = clientId
      this.params['orWhere.recommend_service.eq'] = clientId
    }
    this.getData()
  },
  mounted() {},
  methods: {
    async getData(index = 1) {
      this.params.index = index
      this.loading = true
      const [res] = await this.$utils.promise(
        this.$api.getData(SERVICE_NAME, this.tableName, this.params)
      )
      this.loading = false

      const data = (res && res.data) || {}
      this.tableData = (data.data || []).map(item => {
        // load_beh_status  贷后状态 1 正常 2 关注 3 次级 4 可疑 5 损失 6已完结
        item.loadStatus = enumRenewalStatus[item.load_beh_status] || '-'
        item.loadStyle = this.enumRenewalStyle[item.load_beh_status] || ''
        return item
      })
      this.tableCount = data.totalCount || 0
    },
    handleView(row) {},
    async handleDownload() {
      const { index, size, ...params } = this.params // eslint-disable-line
      const exportParams = {
        service: SERVICE_NAME,
        table: this.tableName,
        params: params,
        filename: '异常提示.xlsx',
      }

      this.exportLoading = true
      await this.$utils.promise(this.$api.exportData(exportParams))
      this.exportLoading = false
    },
  },
}
</script>

<style lang="scss" scoped></style>
